package com.example.terminal.view.edit;

import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.terminal.R;
import com.example.terminal.base.BaseActivity;
import com.example.terminal.util.ToastUtils;
import com.example.terminal.view.dialog.AlertDialog;


/**
 * 拆板操作
 */
public class EditorActivity extends BaseActivity {

    private static EditText mEt_Contot;
    private TextView mTv_Tiltle;
    private Button mBt_Cancel;
    private Button mBt_Sure;
    private static EditorCallbackListener mEditorCallback;
    private static String mContent;

    @Override
    protected int setContentView() {
        return R.layout.editor_view;
    }


    protected void initView() {
        mEt_Contot = findViewById(R.id.mEt_Contot);
        mTv_Tiltle = findViewById(R.id.mTv_Tiltle);
        mBt_Cancel = findViewById(R.id.mBt_Cancel);
        mBt_Sure = findViewById(R.id.mBt_Sure);
    }

    @Override
    protected void initData() {
        if (!TextUtils.isEmpty(mContent))
            mTv_Tiltle.setText(mContent);

        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);

        openKeyBoard(mEt_Contot);

        setEvent();
    }

    private void setEvent() {
        mEt_Contot.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    return clickFinish();
                }
                return false;
            }
        });
    }

    /**
     * 点击确定
     *
     * @param view
     * @return
     */
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mBt_Cancel:
                onFinish();
                break;
            case R.id.mBt_Sure:
                clickFinish();
                break;
        }
    }

    public boolean clickFinish() {
        String content = mEt_Contot.getText().toString();

        if (TextUtils.isEmpty(content)) {
            ToastUtils.showToast(R.string.toast_content_null);
            return true;
        } else {
            if (mEditorCallback != null)
                mEditorCallback.onSubmit(TextUtils.isEmpty(content) ? mContent : content);

            onFinish();
            return false;
        }
    }

    /**
     * 打开页面
     *
     * @param baseActivity
     * @param editorCallback
     */
    public static void openActivity(BaseActivity baseActivity, EditorCallbackListener editorCallback, String content) {
        Intent intent = new Intent(baseActivity, EditorActivity.class);

        mEditorCallback = editorCallback;
        mContent = content;

        baseActivity.turnActForResult(intent);
    }

    public interface EditorCallbackListener {
        void onSubmit(String content);
    }

    @Override
    protected void changeSystemUi() {

    }

    @Override
    protected void onDestroy() {
        mEditorCallback = null;
        super.onDestroy();
    }
}
